import QtQuick 1.0
import Qt 4.7
import QtMobility.location 1.1
import "boris.js" as BorisGlobal

Item {
    id: root
    width: parent.width
    height: parent.height
    opacity: 1


    property double latitude: phoneModel.latitude()
    property double longitude: phoneModel.longitude()

    property double t_latitude: phoneModel.latitude()
    property double t_longitude: phoneModel.longitude()

    property int activeBorisIndex: 0

    function update(borisIndex) {
        if(borisIndex < 0) {
            borisIndex = borisIndex + mainWindow.borisItemCount();
        }
        activeBorisIndex = borisIndex % mainWindow.borisItemCount()
        console.log("MapViewer.qml update(" + borisIndex + ", " + activeBorisIndex + "), con: " + map.connectivityMode + ", offline: " + Map.OfflineMode);
        //activeBorisIndex = borisIndex

        positionMarker.coordinate.latitude = latitude
        positionMarker.coordinate.longitude = longitude

        t_latitude = borisMarker.coordinate.latitude = mainWindow.borisItem(activeBorisIndex).latitude
        t_longitude = borisMarker.coordinate.longitude = mainWindow.borisItem(activeBorisIndex).longitude

        borisName.text = BorisGlobal.toOneLineString(mainWindow.borisItem(activeBorisIndex).name)
        borisDistance.text = BorisGlobal.formatDistance(mainWindow.borisItem(activeBorisIndex).distance)

        map.center.latitude = (latitude + t_latitude) / 2
        map.center.longitude = (longitude + t_longitude) / 2
        //map.addMapObject(pushPins.objects.)

    }


    //focus : true

    Connections {
        target: phoneModel
        onPositionChanged: {
            latitude = phoneModel.latitude()
            longitude = phoneModel.longitude()
            if(mainWindow.borisItemCount() > 0) {
                borisDistance.text = BorisGlobal.formatDistance(mainWindow.borisItem(activeBorisIndex).distance);
            }
            //console.log("MapViewer.qml [" + latitude + ", " + longitude + "]")
        }
    }

    MapGroup {
        id: pushPins
        MapImage {
            id: pushPin
            coordinate: Coordinate {
                // Greenwich Royal Observatory
                latitude: 51.477811
                longitude: -0.001475
            }
            source: "qrc:///images/cycle-hire-pushpin-icon.png"
        }
    }

    Map {
        id: map
        plugin : Plugin { name : "nokia" }
        size.width: parent.width
        size.height: parent.height
        zoomLevel: 12
        connectivityMode: Map.OfflineMode

        center: Coordinate {
            // Greenwich Royal Observatory
            latitude: 51.477811
            longitude: -0.001475
        }

        MapImage {
            id: positionMarker
            coordinate: Coordinate {
                // Greenwich Royal Observatory
                latitude: 51.477811
                longitude: -0.001475
            }
            source: "qrc:///images/locator_green.svg"
        }

        MapImage {
            id: borisMarker
            coordinate: Coordinate {
                // Greenwich Royal Observatory
                latitude: 51.477811
                longitude: -0.001475
            }
            source: "qrc:///images/locator_red.svg"
        }

        onActiveFocusChanged: {
            console.log("MapView focus changed. scope = " + root.activeFocus + "; map: " + map.activeFocus)
        }
        /*
        MapCircle{
            id: borisMarker
            center : Coordinate {
                // Greenwich Royal Observatory
                latitude : 51.477811
                longitude : -0.001475
            }
            radius : 1000.0 * Math.pow(2, (8 - map.zoomLevel))
            color : "red"
        }
         */
    }

    Rectangle {
        anchors.top: parent.top
        anchors.left: parent.left
        height: borisName.height
        width: borisName.width
        opacity: 0.6
        Text {
            id: borisName
            text: "Royal Observatory<br/>Greenwich"
            font.pointSize: 9
            font.bold: true
            //opacity: 0.9
        }
    }

    Rectangle {
        anchors.bottom: parent.bottom
        anchors.right: parent.right
        height: borisDistance.height
        width: borisDistance.width
        opacity: 0.6
        Text {
            id: borisDistance
            text: "0 m"
            font.pointSize: 14
            font.bold: true
            //opacity: 0.9
        }
    }

    MouseArea {

        anchors.fill : parent

        property bool mouseDown : false
        property int lastX : -1
        property int lastY : -1

        hoverEnabled : true

        onPressed : {
            mouseDown = true 
            lastX = mouse.x
            lastY = mouse.y
        }
        onReleased : { 
            mouseDown = false 
            lastX = -1
            lastY = -1
        }
        onPositionChanged: {
            if (mouseDown) {
                var dx = mouse.x - lastX
                var dy = mouse.y - lastY
                map.pan(-dx, -dy)
                lastX = mouse.x
                lastY = mouse.y
            }
        }
        onDoubleClicked: {
            map.center = map.toCoordinate(Qt.point(mouse.x, mouse.y))
            map.zoomLevel += 1
        }
    }

    Keys.onCallPressed: {
        console.log("call")
    }

    Keys.onVolumeDownPressed: {
        console.log("zoom out")
        map.zoomLevel += 1
    }

    Keys.onVolumeUpPressed: {
        console.log("zoom in")
        map.zoomLevel -= 1
    }


    Keys.onPressed: {
        console.log("MapView key pressed: " + event.key + "; modifiers: " + event.modifiers)
        if (event.key == 105) {
            map.zoomLevel += 1
            console.log("+ (" + map.zoomLevel + ")")
        } else if (event.key == 107) {
            map.zoomLevel -= 1
            console.log("- (" + map.zoomLevel + ")")
        } else if (event.key == 49) {
            map.zoomLevel = 12
            update(activeBorisIndex)
        } else if (event.key == 16777221) {
            update(activeBorisIndex + 1)
        } else if (event.key == 16777219) {
            update(activeBorisIndex - 1)
        } else if (event.key == Qt.Key_Left) {
            if(event.modifiers) {
                event.accepted = false;
            } else {
                map.pan(-10, 0);
                event.accepted = true;
            }
        } else if (event.key == Qt.Key_Right) {
            if(event.modifiers) {
                event.accepted = false;
            } else {
                map.pan(10, 0);
                event.accepted = true;
            }
        } else if (event.key == Qt.Key_Up) {
            console.log("^")
            map.pan(0, -10)
        } else if (event.key == Qt.Key_Down) {
            console.log("v")
            map.pan(0, 10)
        } else if (event.key == Qt.Key_2) {
            if (map.mapType == Map.StreetMap) {
                map.mapType = Map.SatelliteMapDay
            } else if (map.mapType == Map.SatelliteMapDay) {
                map.mapType = Map.StreetMap
            }
        }
    }
}
